// components/Dialog/dialog.js
var WxParse = require('../../wxParse/wxParse.js');
var common = require('../../../common.js');
var businessLogic = require('../../../NetWork/Model/MainbusinessLogic.js')

Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  /**
   * 组件的属性列表
   * 用于组件自定义设置
   */
  properties: {

  },

  /**
   * 私有数据,组件的初始数据
   * 可用于模版渲染
   */
  data: {
    // 弹窗显示控制
    isShow: false,
    timu: "",
    datiNum: "",
    xiaotiNum: 0,
    titype: "",
    mydaan: "",
    timulength: "",
    remarktype:"",
    csub:"",
    orgid: "",
    userid: "",
    role: "",
    peopleid: ""

  },

  /**
   * 组件的方法列表
   * 更新属性和数据的方法与更新页面数据的方法类似
   */
  methods: {
    /*
     * 公有方法
     */
  
    tiantimu(timu, remarktype, examid) {
      var timulength = timu.csub.length;
      this.setData({
        timu: timu,
        timulength: timulength,
        remarktype: remarktype,
        examid: examid,
        rubric: timu,
        csub: timu.csub[0]
      })
      console.log(this.data.remarktype);
      this.xianshiti(0)
    },
    xianshiti(numb) {

      var article = this.data.timu.csub[numb].title;//把题干通过富文本显示器挂上去
      var that = this;
      WxParse.wxParse('article', 'html', article, that, 20);

      var xiaotitypecode = this.data.timu.csub[numb].typecode_dictionary_title;
      console.log(this.data.timu.csub[numb].id);
      this.setData({
        testid: this.data.timu.csub[numb].id,
      })
      this.dataCenter();
      this.findInfoTempListByClassid(this.data.testid);
      console.log("testid", this.data.testid);

      if (xiaotitypecode == "单选题" || xiaotitypecode == "多选题") {

        this.setData({
          titype: "选择题",
        })

        this.ChoiceQuestion = this.selectComponent("#ChoiceQuestion");
        this.ChoiceQuestion.tiantimu(this.data.timu.csub[numb],this.data.remarktype);

      } else {
        this.setData({
          titype: "填空题",
        })
        this.GapFilling = this.selectComponent("#GapFilling");
        this.GapFilling.tiantimu(this.data.timu.csub[numb],this.data.remarktype);

      }

    },
    mydan() {
      var mydaan = "";

      this.ChoiceQuestion = this.selectComponent("#ChoiceQuestion");
      this.GapFilling = this.selectComponent("#GapFilling");

      if (this.ChoiceQuestion.mydaan() == "" || this.ChoiceQuestion.mydaan() == null) {
        mydaan = this.GapFilling.mydaan();
        if (this.GapFilling.mydaan() == "" || this.GapFilling.mydaan() == null) {
          mydaan = "";
        }
      } else {
        mydaan = this.ChoiceQuestion.mydaan();
      }
      return mydaan;
    },
    shangyiti() {
      var xiaotiNum = this.data.xiaotiNum - 1;
      if (xiaotiNum >= 0) {
        this.xianshiti(xiaotiNum);
        // console.log(xiaotiNum);
        let rubric = this.data.rubric;
        this.setData({
          xiaotiNum: xiaotiNum,
          csub: rubric.csub[xiaotiNum]
        })
      }else{
        common.showToast("第一题", "none");        
      }


    }, xiayiti() {
      var xiaotiNum = this.data.xiaotiNum + 1;      
      if (xiaotiNum <= this.data.timulength-1) {
        this.xianshiti(xiaotiNum);
        this.setData({
          xiaotiNum: xiaotiNum
        })
      }else{
        common.showToast("最后一题","none");
      }
    }, 
  
    zhtext(nextext){
      console.log(nextext);
   
      if (this.data.titype == "选择题") {
        this.ChoiceQuestion.zxtext(nextext.newstext);

      } else if (this.data.titype == "综合题") {
        this.Comprehensive.zhtext(nextext.newstext);

      } else {
        // 填空题
        this.GapFilling.tktext(nextext.newstext);
      }
    },

    sure(){
      this.mydan();//获取答案

      if (this.data.mydata == '' || this.data.mydata == '请选择答案' || this.data.mydata == undefined) {
        common.showToast("请选择答案", "none");
      } else {
        this.setData({
          queren: '已确认',
          color: true,
        })
        var sureAnswer = [];
        sureAnswer["mydata"] = this.data.mydata;
        sureAnswer["rubric"] = this.data.rubric;
        sureAnswer["csub"] = this.data.csub;
        return sureAnswer;
      }

    },
    //2.通过组件获取答案
    mydan() {
      var mydaan = "";
      this.ChoiceQuestion = this.selectComponent("#ChoiceQuestion");
      this.GapFilling = this.selectComponent("#GapFilling");
      if (this.data.titype == "填空题") {
        mydaan = this.GapFilling.mydaan();
        if (this.GapFilling.mydaan() == "" || this.GapFilling.mydaan() == null) {
          mydaan = "";
        }
      } else {
        mydaan = this.ChoiceQuestion.mydaan();
      }
      this.setData({
        mydata: mydaan
      })
    },//2.取出个人信息，机构信息，题目信息
    dataCenter: function () {
      var orgid = common.getStorageSync("orgid")
      var userid = common.getStorageSync("userid")
      var role = common.getStorageSync("role");
      var peopleid = common.getStorageSync("userid");
      var name = common.getStorageSync("userid");
      this.setData({
        orgid: orgid,
        userid: userid,
        role: role,
        peopleid: peopleid,
        name:name
      });
    },
    //8.3.1 查询试题是否被做过及试题的答案（看是否有返回值 ?）（已测试）	
    findInfoTempListByClassid: function (testid) {
      console.log(testid);
      let renid = "";
      if (this.data.peopleid == "" || this.data.peopleid == undefined) {
        renid = this.data.userid;
      } else {
        renid = this.data.peopleid;
      }
      let params = { "orgid": this.data.orgid, "userid": renid, "searchField": "peopleid,examid,testid", "searchValue": "" + this.data.name + "," + this.data.examid + "," + testid + "" }
      //console.log("8.3.1",params)
      businessLogic.DoneAnd(params, (res) => {
        // common.showLoading("加载中", "loading");
        // console.log(res);
        if (res == undefined) {
          this.setData({
            shitiided: "",//查询试题id
          })
        } else {
          this.setData({
            shitiided: res.id,//查询试题id
          })
          this.Newstext(res)//试题的答案返回给
        }
      });
    },
    Newstext(res) {
      console.log("试题的答案", res);
      if (res.say == "" || res.say == undefined) {
        this.setData({
          point: res.point,
          pinjia: "",
          defen: res.score,
        })
      } else {
        this.setData({
          point: res.point,
          pinjia: res.say
        })
      }

      if (this.data.titype == "选择题") {
        this.ChoiceQuestion.zxtext(res.newstext);

      } else if (this.data.titype == "综合题") {
        // this.Comprehensive.chuli(this.data.examid);

      } else {
        // 填空题
        if (res.newstext == "undefined") {

        } else {
          this.GapFilling.tktext(res.newstext);
        }

      }

    }, 
    /*
    * 内部私有方法建议以下划线开头
    * triggerEvent 用于触发事件
    */
    _cancelEvent() {
      //触发取消回调
      this.triggerEvent("cancelEvent")
    },
    _confirmEvent() {
      //触发成功回调
      this.triggerEvent("confirmEvent");
    }
  }
})